/*******************************************************************************

      ---------========= DSx86 Configuration Editor =========---------
                               by RyouArashi
                        http://ryouarashi.blogspot.com


              ---------========= License Agreement =========---------

Copyright 2010 RyouArashi. All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:

   1. Redistributions of source code must retain the above copyright notice,
      this list of conditions and the following disclaimer.

   2. Redistributions in binary form must reproduce the above copyright notice,
      this list of conditions and the following disclaimer in the documentation
      and/or other materials provided with the distribution.

THIS SOFTWARE IS PROVIDED BY RyouArashi ``AS IS'' AND ANY EXPRESS OR IMPLIED
WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
EVENT SHALL RyouArashi OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

*******************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>

#include "VERSION.H"
#include "CONST.H"
#include "CURSOR.H"
#include "PROFILES.H"
#include "INIFILE.H"
#include "UTIL.H"
#include "SCANCODE.H"

/* Locals ********************************************************************/

unsigned char Win32 = 0;

/* Functions ******************************************************************/

/*
  Display Welcome Message
*/
unsigned DisplayMessage()
{
  unsigned ret = 1;

  /* Welcome Message */
  cprintf(
    "%s\r\n"
    "Copyright (c) 2010 RyouArashi\r\n",
    APP_NAME VER
  );
  
  /* Win32 Compatibility Mode */
  if (Win32)
  {
    cprintf("\r\nWin32 Compatibility Mode enabled.\r\n");
  }

  /* Beta Warning */
  cprintf(
    "\r\n"
    "WARNING:\r\n"
    "This Program is still in beta\r\n"
    "state and may contain errors.\r\n"
    "It is recommended to backup\r\n"
    "your files before using this\r\n"
    "program.\r\n"
    "\r\n"
    "[ENTER ] Run DSx86Cfg\r\n"
    "[ESCAPE] Exit\r\n"
  );

  /* Wait for Input */
  while (1)
  {
    char c = GetKey();

    if (c == K_ENTER)  { ret = 1; break; }
    if (c == K_ESCAPE) { ret = 0; break; }
  }

  return ret;
}

/*
  Remove Temp File
*/
void RemoveTempFile()
{
  if (remove(ConfigTempName))
  {
    perror("\r\nError Removing Temp File:\r\n");
  }
}

/*******************************************************************************
 * Program Entry Point
 ******************************************************************************/
int main (int argc, char *argv[])
{
  struct ProfileList * List;

  /* Check for Win32 Compatibility switch */
  if (argc == 2)
  {
    if (strcmp(argv[1], "-win32") == 0)
    {
      Win32 = 1;
      EnableWin32Filter();
    }
  }

  /* Init and Hide Cursor */
  InitCursor();
  SetCursorMode(CURSOR_HIDDEN);

  /* Init Screen */
  clrscr();
  textcolor(LIGHTGRAY);

  /* Display Welcome Message */
  if (DisplayMessage())
  {
    /*  Parse Config File */
    List = ParseConfigFile();

    /* Check Parse Result */
    if (List)
    {
      unsigned DelTemp;

      /* Start Editor */
      if (RunEditor(List))
      {
        /* Editor returned, save new Config */
        DelTemp = WriteConfigFile(List);
      }
      else
      {
        /* Editor returned, but don't save */
        DelTemp = 1;
      }

      /* Finished, free List*/
      ProfileList_Free(List);

      /* Delete Temp File */
      if (DelTemp)
        RemoveTempFile();
      /* Preserve Temp File if saving failed */
      else
        cprintf(
          "\r\n"
          "\r\nPrevious contents of Config File are"
          "\r\nbacked up in %s.", ConfigTempName
        );
    }
  }

  /* Restore Screen */
  SetCursorMode(CURSOR_DEFAULT);
  textcolor(LIGHTGRAY);

  cprintf("\r\n\r\nPress any key to continue...\r\n");
  getch();

  return 0;
}